package com.app.qtjk.lsh.ctrl;
import java.util.ArrayList;
import java.util.HashMap;

import com.app.common.utils.FilterUtil;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.controller.annotations.CTRL;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;


@CTRL("UserCtrl")
public class UserCtrl extends BaseDomainCtrl {

	private static SwordLogUtils log = SwordLogUtils.getLogger(UserCtrl.class);

	/**
	 * 登录接口
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public IResData login(IReqData req) throws Exception{

		log.debug("开始进入 UserCtrl_login 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		boolean flag = true;
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
				flag = false;
			}
			if (flag == true) {
				//调用检查是否注册service
				res = (IResData) SwordServiceUtils.callService("UserService_checkRegister", req);
				if ((Boolean) res.get("registerState")==true) {
					hashMap.put("registerState", "true");
					//当设备是绑定设备时，去判断密码是否正确
					if ((Boolean) res.get("deviceState")==true) {
						hashMap.put("deviceState", "true");
						res = (IResData) SwordServiceUtils.callService("UserService_checkPassword", req);
						if ((Boolean) res.get("verifyPWD")==true) {
							String userid=StringUtil.parse2String(res.get("userid"));
							hashMap.put("loginState", "true");
							hashMap.put("userId", userid);
							hashMap.put("msg", "密码正确,登录成功！");
						}else {
							hashMap.put("loginState", "false");
							hashMap.put("msg", "密码不正确,登录失败！");
						}
					}else {
						hashMap.put("deviceState", "false");
					}
				}else {
					hashMap.put("registerState", "false");
					hashMap.put("msg", "未注册，用户不存在！");
				}
			}
			hashMap.put("returnStatus", "true");
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}
	
	/**
	 * 登录接口
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public IResData ioslogin(IReqData req) throws Exception{
		
		log.debug("开始进入 UserCtrl_ioslogin 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		boolean flag = true;
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
				flag = false;
			}
			if (flag == true) {
				//调用检查是否注册service
				res = (IResData) SwordServiceUtils.callService("UserService_checkRegisterIos", req);
				if ((Boolean) res.get("registerState")==true) {
					hashMap.put("registerState", "true");
					//当设备是绑定设备时，去判断密码是否正确
					if ((Boolean) res.get("deviceState")==true) {
						hashMap.put("deviceState", "true");
						res = (IResData) SwordServiceUtils.callService("UserService_checkPassword", req);
						if ((Boolean) res.get("verifyPWD")==true) {
							String userid=StringUtil.parse2String(res.get("userid"));
							hashMap.put("loginState", "true");
							hashMap.put("userId", userid);
							hashMap.put("msg", "密码正确,登录成功！");
						}else {
							hashMap.put("loginState", "false");
							hashMap.put("msg", "密码不正确,登录失败！");
						}
					}else {
						hashMap.put("deviceState", "false");
					}
				}else {
					hashMap.put("registerState", "false");
					hashMap.put("msg", "未注册，用户不存在！");
				}
			}
			hashMap.put("returnStatus", "true");
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}
	
 	/**
 	 * 更换绑定设备
 	 * @param req
 	 * @return
 	 * @throws Exception
 	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public IResData changeDevice(IReqData req) throws Exception{

		log.debug("开始进入 UserCtrl_changeDevice 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			} else {
				// 获取前台post的数据
				String phoneType = ReqJsonUtil.getPostParam(req,"phonetype");
				if ("android".equals(phoneType)) {
					String codeAndroid="";
					res = (IResData) SwordServiceUtils.callService("UserService_checkRegister", req);
					//用户未注册时
						if ((Boolean) res.get("registerState")==false) {
							codeAndroid = "0001";
						}else{
							if ((Boolean) res.get("deviceState")==true) {
								codeAndroid = "0004";
							}else {
								res = (IResData) SwordServiceUtils.callService("UserService_checkPassword", req);
								//密码不正确时
								if ((Boolean) res.get("verifyPWD")==false) {
									codeAndroid = "0002";
								}else {
									//调用更新绑定设备接口
									res = (IResData) SwordServiceUtils.callService("UserService_updateDevice", req);
									if ((Boolean) res.get("deviceAndroidSfExist")==true) {
										codeAndroid = "0007";
									}else {
										if ((Boolean) res.get("deviceSfkChange")==false) {
											codeAndroid = "0003";
										}else {
											if ((Boolean) res.get("deviceChanged")==false) {
												codeAndroid = "0005";
											}else {
												codeAndroid = "0006";
											}
										}
									}
								}
							}	
						}
					hashMap.put("code", codeAndroid);
				}
				if ("ios".equals(phoneType)) {
					String codeIos="";
					res = (IResData) SwordServiceUtils.callService("UserService_checkRegisterIos", req);
					//用户未注册时
					if ((Boolean) res.get("registerState")==false) {
						codeIos = "0001";
					}else{
						res = (IResData) SwordServiceUtils.callService("UserService_checkPassword", req);
						//密码不正确时
						if ((Boolean) res.get("verifyPWD")==false) {
							codeIos = "0002";
						}else {
							//调用更新绑定设备接口
							res = (IResData) SwordServiceUtils.callService("UserService_updateDevice", req);
							if ((Boolean) res.get("deviceSfkChange")==false) {
								codeIos = "0003";
							}else {
								if ((Boolean) res.get("deviceChanged")==false) {
									codeIos = "0005";
								}else {
									codeIos = "0006";
								}
							}
						}
					}
					hashMap.put("code", codeIos);
				}
				hashMap.put("returnStatus", "true");
			}
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		return res;
	}
	
	/**
	 * 用户签到接口
	 * @param req
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public IResData sign(IReqData req){
		
		log.debug("开始进入 UserCtrl_sign 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			} else {
				IResData checkres= (IResData) SwordServiceUtils.callService("UserSignService_checkSfSign", req);
				if ((Boolean) checkres.get("signExist") == true) {
					res= (IResData) SwordServiceUtils.callService("UserSignService_commonSign", req);
				}else {
					res= (IResData) SwordServiceUtils.callService("UserSignService_newSignRecord", req);
				}
				hashMap.put("returnStatus", "true");
				hashMap.put("todayHaveSigned", StringUtil.parse2String(res.get("todayHaveSigned")));
				hashMap.put("signSuccess", StringUtil.parse2String(res.get("signSuccess")));
				hashMap.put("wealthValueAdd", StringUtil.parse2String(res.get("wealthValueAdd")));
			}	
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
		
		return res;
	}
	
	/**
	 * 得到用户连续签到次数
	 * @param req
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public IResData getSignCount(IReqData req){
		
 		log.debug("开始进入 UserCtrl_getSignCount 方法。");
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			} else {
				IResData checkres= (IResData) SwordServiceUtils.callService("UserSignService_checkSfSign", req);
				if ((Boolean) checkres.get("signExist") == true) {
					hashMap.put("todayHaveSigned", StringUtil.parse2String(checkres.get("todayHaveSigned")));
					hashMap.put("signCount", checkres.get("signCount"));
				}else {
					hashMap.put("todayHaveSigned","false");
					hashMap.put("signCount", 0);
				}
				hashMap.put("returnStatus", "true");
			} 
		} catch (Exception e) {
			log.debug("---------------系统发生异常！---------------");
			e.printStackTrace();
			hashMap.put("returnStatus", "false");
			hashMap.put("msg", "出现异常！");
		} finally {
			commonMap.put("commonData", hashMap);
			commonList.add(commonMap);
			res.addListWithDataName("returnData", commonList);
		}
			
		return res;
	}	
}